#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

import os
import sys
import shutil

ht = {}
for file_name in os.listdir(sys.argv[1]):
    fanhao = file_name.split(".")[1]
    if fanhao in ht:
        ht[fanhao] += 1
    else:
        ht[fanhao] = 1
for file_name in os.listdir(sys.argv[1]):
    split_file = file_name.split(".")
    fanhao = split_file[1]
    extend = split_file[-1]
    os.makedirs(sys.argv[2] + "/" + fanhao[:4].upper() + "-" + fanhao[-3:], exist_ok=True)
    if ht[fanhao] == 2:
        shutil.move(sys.argv[1] + "/" + file_name,
                    sys.argv[2] + "/" + fanhao[:4].upper() + "-" + fanhao[-3:] + "/" + fanhao[
                                                                                       :4].upper() + "-" + fanhao[
                                                                                                           -3:] + "." + extend)
    else:
        if split_file[-2].endswith("A"):
            shutil.move(sys.argv[1] + "/" + file_name,
                        sys.argv[2] + "/" + fanhao[:4].upper() + "-" + fanhao[-3:] + "/" + fanhao[
                                                                                           :4].upper() + "-" + fanhao[
                                                                                                               -3:] + "-part1." + extend)
        elif split_file[-2].endswith("B"):
            shutil.move(sys.argv[1] + "/" + file_name,
                        sys.argv[2] + "/" + fanhao[:4].upper() + "-" + fanhao[-3:] + "/" + fanhao[
                                                                                           :4].upper() + "-" + fanhao[
                                                                                                               -3:] + "-part2." + extend)
        else:
            shutil.move(sys.argv[1] + "/" + file_name,
                        sys.argv[2] + "/" + fanhao[:4].upper() + "-" + fanhao[-3:] + "/" + fanhao[
                                                                                           :4].upper() + "-" + fanhao[
                                                                                                               -3:] + "." + extend)
